﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.ComponentModel;
using System.Web.Configuration;

namespace HospitalSystem
{
    public partial class servicereceived : System.Web.UI.Page
    {
        public static List<string> x = new List<string>();
        DataTable dt = new DataTable();
        DataTable dx = new DataTable();
   
        protected void Page_Load(object sender, EventArgs e)
        {

            String ShowValue = (string)Session["CID"];
            Label1.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            Label3.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            Label2.Text = ShowValue3;


            String ShowValue4 = (string)Session["CaseNo"];
            Label4.Text = ShowValue4;
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }
        protected void BindData()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("select Name from DisabilityType", scon);
            scon.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                DropDownList1.Items.Add(reader["Name"].ToString());
            }
            reader.Close();
            scon.Close();
        }
        protected void Cal1_SelectionChanged(object sender, EventArgs e)
        {

            Calendar cal = (Calendar)sender;
            TextBox text1 = (TextBox)((GridViewRow)cal.Parent.Parent).FindControl("TIMS");
            text1.Text = cal.SelectedDate.ToShortDateString();
            cal.Visible = false;
        }
        protected void Cal2_SelectionChanged(object sender, EventArgs e)
        {
            Calendar cal = (Calendar)sender;
            TextBox text2 = (TextBox)((GridViewRow)cal.Parent.Parent).FindControl("TIME");
            text2.Text = cal.SelectedDate.ToShortDateString();
            cal.Visible = false;
        }
        protected void ButtonTIMS_Click(object sender, EventArgs e)
        {
            ImageButton IM1 = (ImageButton)sender;
            Calendar cal1 = (Calendar)((GridViewRow)IM1.Parent.Parent).FindControl("Calendar1");
            cal1.Visible = true;
        }
        protected void ButtonTIME_Click(object sender, EventArgs e)
        {
            ImageButton IM2 = (ImageButton)sender;
            Calendar cal2 = (Calendar)((GridViewRow)IM2.Parent.Parent).FindControl("Calendar2");
            cal2.Visible = true;
        }
        private void SetInitialRow(int xs)
        {
            dt.Columns.Add("RowNumber");
            for (int i = 1; i <= xs; i++)
            {
                dt.Rows.Add(i.ToString());
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            
            
            int rowNo = Convert.ToInt32(TextBox4.Text);
            SetInitialRow(rowNo);
            x.Clear();
            SqlCommand cmd = new SqlCommand("SELECT MaterialDesc FROM Material WHERE MaterialCode IN (SELECT MaterialCode FROM Material_DisableType WHERE DisabilityType IN (SELECT DisabilityCode FROM DisabilityType WHERE Name ='" + DropDownList1.SelectedItem.Text + "'))", scon);
            scon.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                x.Add(reader["MaterialDesc"].ToString());
            }
            reader.Close();
            scon.Close();
            foreach (GridViewRow row in GridView1.Rows)
            {
                DropDownList DDL = (DropDownList)GridView1.Rows[row.RowIndex].Cells[1].FindControl("MAT");
                for(int c = 1; c < x.Count;c++)
                {
                    DDL.Items.Add(x[c]);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            
            
            if (dx.Columns.Count <= 0)
            {
                dx.Columns.Add("CID");
                dx.Columns.Add("CaseNo");
                dx.Columns.Add("ToolCode");
                dx.Columns.Add("StartTime");
                dx.Columns.Add("EndTime");
                dx.Columns.Add("Provider");
                dx.Columns.Add("Result");
                dx.Columns.Add("RecorderID");
                dx.Columns.Add("EvalDateTime");
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                scon.Open();
                DropDownList DDL = (DropDownList)GridView1.Rows[row.RowIndex].Cells[1].FindControl("MAT");
                TextBox tims = (TextBox)row.FindControl("TIMS");
                TextBox time = (TextBox)row.FindControl("TIME");
                TextBox org = (TextBox)row.FindControl("ORG");
                SqlCommand cmd = new SqlCommand("SELECT MaterialCode FROM Material WHERE MaterialDesc = @sj", scon);
                cmd.Parameters.AddWithValue("@sj", DDL.SelectedItem.Text);
                    SqlDataReader reader2 = cmd.ExecuteReader();
                    RadioButtonList RBLT = (RadioButtonList)row.FindControl("RBL");
                    if (reader2.Read()!=false)
                    {
                        dx.Rows.Add(Label1.Text, Label4.Text, reader2["MaterialCode"].ToString(), tims.Text, time.Text, org.Text, RBLT.SelectedValue.ToString(), Label5.Text, DateTime.Now.ToString());
                    }
                    else
                    {
                        MessageBox("กูงง!!");
                    }
                scon.Close();
            }
            GridView2.DataSource = dx;
            GridView2.DataBind();
            SqlCommand command2 = new SqlCommand("delete from [Tools] where CID=@CID and CaseNo=@CaseNo", scon);
            scon.Open();
            command2.Parameters.AddWithValue("@CID", Label1.Text);
            command2.Parameters.AddWithValue("@CaseNo", Label4.Text);
            command2.ExecuteNonQuery();
            scon.Close();



            try
            {
                for (int i = 0; i < dx.Rows.Count; i++)
                {
                    SqlCommand command = new SqlCommand("INSERT INTO [Tools] (CID,CaseNo,ToolCode,StartTime,EndTime,Provider,Result,RecorderID,EvalDateTime) VALUES('" + dx.Rows[i]["CID"].ToString() + "','" + dx.Rows[i]["CaseNo"].ToString() + "','" + dx.Rows[i]["ToolCode"].ToString() + "','" + dx.Rows[i]["StartTime"].ToString() + "','" + dx.Rows[i]["EndTime"].ToString() + "','" + dx.Rows[i]["Provider"].ToString() + "','" + dx.Rows[i]["Result"].ToString() + "','" + dx.Rows[i]["RecorderID"].ToString() + "','" + dx.Rows[i]["EvalDateTime"].ToString() + "')", scon);
                    scon.Open();
                    command.ExecuteNonQuery();
                    scon.Close();









                    SqlCommand command33 = new SqlCommand("INSERT INTO [Tools_log] (CID,CaseNo,ToolCode,StartTime,EndTime,Provider,Result,RecorderID,EvalDateTime) VALUES('" + dx.Rows[i]["CID"].ToString() + "','" + dx.Rows[i]["CaseNo"].ToString() + "','" + dx.Rows[i]["ToolCode"].ToString() + "','" + dx.Rows[i]["StartTime"].ToString() + "','" + dx.Rows[i]["EndTime"].ToString() + "','" + dx.Rows[i]["Provider"].ToString() + "','" + dx.Rows[i]["Result"].ToString() + "','" + dx.Rows[i]["RecorderID"].ToString() + "','" + dx.Rows[i]["EvalDateTime"].ToString() + "')", scon);
                    scon.Open();
                    command33.ExecuteNonQuery();
                    scon.Close();



                }
            }
            catch
            {
                MessageBox("error อะไรซักอย่างแหละ");
            }
        }

    }
}